package com.cuit.zuke.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cuit.zuke.system.dao.SysAdminRoleDao;
import com.cuit.zuke.system.entity.SysAdminRoleEntity;
import com.cuit.zuke.system.service.SysAdminRoleService;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @program: zuke
 * @description: 管理员与角色对应关系
 * @author: KamTang
 * @create: 2022-04-27 14:21
 **/
@Service
public class SysAdminRoleServiceImpl extends ServiceImpl<SysAdminRoleDao, SysAdminRoleEntity> implements SysAdminRoleService {
    /**
     * 添加or修改
     *
     * @param userId 用户ID
     * @param roleIdList 角色ID列表
     */
    @Override
    public void saveOrUpdate(Long userId, List<Long> roleIdList) {
        // 删除用户与角色的关系，避免重复
        this.removeByIds(Collections.singleton(userId));
        if(roleIdList == null || roleIdList.size() == 0){
            return ;
        }
        // 保存管理员与角色的关系
        for (Long roleId: roleIdList) {
            SysAdminRoleEntity adminRole = new SysAdminRoleEntity();
            adminRole.setUserId(userId);
            adminRole.setRoleId(roleId);
            this.save(adminRole);
        }
    }

    /**
     * 根据用户ID，获取角色ID列表
     *
     * @param userId 用户ID
     * @return List
     */
    @Override
    public List<Long> queryRoleIdList(Long userId) {
        return baseMapper.queryRoleIdList(userId);
    }

    /**
     * 根据角色ID数组，批量删除
     *
     * @param roleIds 角色
     */
    @Override
    public void deleteBatch(Long[] roleIds) {
        baseMapper.deleteBatch(roleIds);
    }
}
